package guardian.web

import com.mls.guardian.common.JsonUtil
import vo.User

class UserController {

    def Map result = ['suc':true,'message':'',data:[]]

    def index() { }

    def userService

    def searchUser(){
        render(contentType:"text/json") {
            result.data = userService.searchAll(new User())
            result.suc = true;
            result
        }
    }

    def saveUser(){
        def newUser = JsonUtil.deserialize(params["content"]);
        User user = session.loginUser;
        User saveUser = new User();
        saveUser.setLoginName(newUser.loginName)
        def users = userService.searchAll(saveUser)
        if(newUser._id==null || newUser._id==""){
            if(users!=null && users.size>0){
                render(contentType:"text/json") {
                    result.message = "账号已存在!"
                    result.suc = false;
                    result
                }
                return
            }
            saveUser.setCreateTime(new Date())
            saveUser.setCreateUser(user.getLoginName())
            saveUser.setPhone(newUser.phone)
            saveUser.setNickName(newUser.nickName)
            userService.save(saveUser);
            render(contentType:"text/json") {
                result.suc = true;
                result
            }
        }else{
            if(users!=null && users.size>0){
                for(User u : users){
                    if(u._id != newUser._id){
                        render(contentType:"text/json") {
                            result.message = "账号已存在!"
                            result.suc = false;
                            result
                        }
                        return
                    }
                }

            }
            saveUser.setUpdateTime(new Date())
            saveUser.setUpdateUser(user.getLoginName())
            saveUser.setPhone(newUser.phone)
            saveUser.setNickName(newUser.nickName)
            saveUser.set_id(newUser._id)
            println newUser
            userService.updateUser(saveUser);
            render(contentType:"text/json") {
                result.suc = true;
                result
            }
        }

    }


    def updatePwd(){
        def pwd = JsonUtil.deserialize(params["content"]);
        User curUser = session.loginUser;
        User user = userService.searchBy_id(curUser._id);
        render(contentType:"text/json") {
            result.suc = true;
            if(user==null){
                result.suc = false;
                result.message = "用户不存在!"
            }else if(user.password == userService.getMd5Pwd(user.loginName,pwd.oldPwd)){
                userService.updatePwd(pwd.newPwd,curUser._id,user.getLoginName())
            }else{
                result.suc = false;
                result.message = "旧密码输入错误!"
            }
            result
        }
    }


    def resetPwd(){
        def user = JsonUtil.deserialize(params["content"]);
        User cuser = session.loginUser;
        render(contentType:"text/json") {
            result.suc = true;
            userService.updatePwd(user.password,user._id,cuser.getLoginName())
            result
        }
    }

    def delUser(){
        User cuser = session.loginUser;
        render(contentType:"text/json") {
            if(cuser._id == params._id){
                result.suc = false;
                result.message = "不能删除当前用户"

            }else{
                result.suc = true;
                userService.del(params._id)
            }
            result
        }
    }
}
